 
package com.zdkj.subway.rca.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.common.core.exceltools.MultipartFileToFile;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.rca.dto.in.UserDto;
import com.zdkj.subway.rca.entity.SwCkOthenTicketRevenueManagement;
import com.zdkj.subway.rca.entity.SwCkOthenTicketRevenueManagementLog;
import com.zdkj.subway.rca.entity.SwCkOthenTicketRevenueManagementTmp;
import com.zdkj.subway.rca.mapper.SwCkOthenTicketRevenueManagementTmpMapper;
import com.zdkj.subway.rca.service.SwCkOthenTicketRevenueManagementLogService;
import com.zdkj.subway.rca.service.SwCkOthenTicketRevenueManagementService;
import com.zdkj.subway.rca.service.SwCkOthenTicketRevenueManagementTmpService;
import com.zdkj.subway.rca.util.JdbcUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 其他运营商票务收入管理临时表  机场线
 *
 * @author stw
 * @date 2020-10-31 13:26:03
 */
@Service
public class SwCkOthenTicketRevenueManagementTmpServiceImpl extends ServiceImpl<SwCkOthenTicketRevenueManagementTmpMapper, SwCkOthenTicketRevenueManagementTmp> implements SwCkOthenTicketRevenueManagementTmpService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SwCkOthenTicketRevenueManagementTmpServiceImpl.class);
    private String parsePattern = "yyyy-MM-dd HH:mm:ss";
	private String parsePatternYMD = "yyyy-MM-dd";
    @Autowired
    private SwCkOthenTicketRevenueManagementService swCkOthenTicketRevenueManagementService;

    @Autowired
    private SwCkOthenTicketRevenueManagementLogService swCkOthenTicketRevenueManagementLogService;

    @Override
    public Boolean saveBatchWithExcel(final File excelFile) throws Exception {
        ExcelReaderBuilder excelReaderBuilder = EasyExcel.read(excelFile);
        List<ReadSheet> sheets = excelReaderBuilder.build().excelExecutor().sheetList();
        boolean flag = false;
        for(int n = 0; n < sheets.size(); n++) {
            String sheetName = sheets.get(n).getSheetName();
            if(sheetName.equals("日期合计")){

                List<Map<Integer, String>> listMap = excelReaderBuilder.sheet(n).headRowNumber(1).doReadSync();
                List<Map<String,String>> lineMap = this.baseMapper.selectLineMap();

                Map<Integer, String> dataLine = listMap.get(0);
                String lineName = "14号线机场段";//dataLine.get(0).substring(5);
                if (CollectionUtil.isNotEmpty(listMap)) {
                    List<SwCkOthenTicketRevenueManagementTmp> listDb = new ArrayList<>();
                    MultipartFileToFile.delteTempFile(excelFile);
                    for (int m = 4; m < listMap.size(); m++) {
                        Map<Integer, String> data = listMap.get(m);
                        // 返回每条数据的键值对 表示所在的列 和所在列的值
                        LOGGER.info("读取到数据:{}", JSON.toJSONString(data));

                        SwCkOthenTicketRevenueManagementTmp tmpObj = new SwCkOthenTicketRevenueManagementTmp();
                        tmpObj.setServiceId(this.getServiceIdByLineCode("54"));

                        tmpObj.setLineCodeTmp(lineName);
                        tmpObj.setServiceIdTmp(tmpObj.getServiceId());

                        tmpObj.setStatisticsDateTmp(data.get(0));//日期

                        tmpObj.setTvmOneTicketCashTmp(data.get(1));
                        tmpObj.setTvmOneTicketAlipayTmp(data.get(2));

                        tmpObj.setTvmOneTicketWechatTmp(data.get(3));
                        tmpObj.setTvmOneTicketXbankTmp(data.get(4));
                        tmpObj.setTvmSubTotalTmp(data.get(5));

                        tmpObj.setBomOneTicketCashTmp(data.get(6));
						tmpObj.setBomOnePosAmountTmp(data.get(7)); //
                        tmpObj.setBomOneTicketPosTmp(data.get(8));
                        tmpObj.setBomOneTicketWechatTmp(data.get(9));
                        tmpObj.setBomSubTotalTmp(data.get(10));

                        tmpObj.setPrefabricatedTicketAmountTmp(data.get(11));
						tmpObj.setPrefabricatedTicketPosAmountTmp(data.get(12));//
                        tmpObj.setEmergencyTicketAmountTmp(data.get(13));
                        tmpObj.setTicketCountingTmp(data.get(14));

                        tmpObj.setDailyTicketCashTmp(data.get(15));
						tmpObj.setDailyTicketPosAmountTmp(data.get(16));//
                        tmpObj.setDailyTicketPosTmp(data.get(17));
                        tmpObj.setDailyTicketWechatTmp(data.get(18));
                        tmpObj.setDailySubTotalTmp(data.get(19));

                        tmpObj.setCommemorativeTicketCashTmp(data.get(20));
						tmpObj.setCommemorativeTicketPosAmountTmp(data.get(21));//
                        tmpObj.setCommemorativeTicketPosTmp(data.get(22));
                        tmpObj.setCommemorativeTicketWechatTmp(data.get(23));
                        tmpObj.setCommemorativeTicketSubTotalTmp(data.get(24));

                        tmpObj.setAppCommemorativeTicketWechatTmp(data.get(25));

                        tmpObj.setCatTicketCashTmp(data.get(26));
						tmpObj.setCatTicketPosAmountTmp(data.get(27));//
                        tmpObj.setCatTicketPosTmp(data.get(28));
                        tmpObj.setCatTicketWechatTmp(data.get(29));
                        tmpObj.setCatTicketTvmsimAlipayTmp(data.get(30));
                        tmpObj.setCatTicketTvmsimWechatTmp(data.get(31));
                        tmpObj.setCatTicketTvmsimXbankTmp(data.get(32));
                        tmpObj.setCatSubTotalTmp(data.get(33));

                        tmpObj.setCatTicketPayCardTmp(data.get(34));
                        tmpObj.setOnlineTicketSalesTmp(data.get(35));

                        tmpObj.setQrCodeTicketAlipayTmp(data.get(36));
                        tmpObj.setQrCodeTicketWechatTmp(data.get(37));
                        tmpObj.setQrCodeTicketUnionpayTmp(data.get(38));
                        tmpObj.setQrCodeSubTotalTmp(data.get(39));

                        tmpObj.setFaceEleBankTmp(data.get(40));
                        //36-38增加应急及互联互通标识
                        tmpObj.setQrConnectivityAlipayTmp(data.get(41));
                        tmpObj.setQrConnectivityWechatTmp(data.get(42));
                        tmpObj.setQrConnectivitySubTotalTmp(data.get(43));

                        tmpObj.setAllOneTrafficCardTmp(data.get(44));

                        tmpObj.setStationTicketLongCashTmp(data.get(45));
                        tmpObj.setStationTicketShortCashTmp(data.get(46));
                        tmpObj.setStationCompensationCashTmp(data.get(47));
                        tmpObj.setReleasePettyCashTmp(data.get(48));
						tmpObj.setSubTotalPosTmp(data.get(49));//
                        tmpObj.setSettlementAmountTmp(data.get(50));
                        tmpObj.setSubTotalTmp(data.get(51));

                       /*
                        tmpObj.setAllOneTrafficCardTmp(data.get(36));

                        tmpObj.setStationTicketLongCashTmp(data.get(37));
                        tmpObj.setStationTicketShortCashTmp(data.get(38));
                        tmpObj.setStationCompensationCashTmp(data.get(39));
                        tmpObj.setReleasePettyCashTmp(data.get(40));
                        tmpObj.setSettlementAmountTmp(data.get(41));
                        tmpObj.setSubTotalTmp(data.get(42));
						*/

                        if (StringUtils.isEmpty(tmpObj.getStatisticsDateTmp())) {
                            continue;
                        }
                        if (!parseDate(tmpObj.getStatisticsDateTmp())) {
                            continue;
                        }
                        final SwCkOthenTicketRevenueManagementTmp obj = ObjectUtils.getDefaultObj(SwCkOthenTicketRevenueManagementTmp.class);
                        BeanUtils.copyProperties(tmpObj, obj, "deptId", "creatorId", "creatorName");
                        checkErr(obj,lineMap);
                        //acc更新
                        String statisticsDate = data.get(0);
                        Long accAlipay = this.baseMapper.selectAccAlipay(statisticsDate);
                        Long accWechat = this.baseMapper.selectAccWechat(statisticsDate);
                        Long accTvmsimAlipay = this.baseMapper.selectAccTvmsimAlipay(statisticsDate);
                        Long accTvmsimWechat = this.baseMapper.selectAccTvmsimWechat(statisticsDate);
                        Long accTvmsimXbank = this.baseMapper.selectAccTvmsimXbank(statisticsDate);
                        obj.setCatTicketPos(accAlipay);
                        obj.setCatTicketWechat(accWechat);
                        obj.setCatTicketTvmsimAlipay(accTvmsimAlipay);
                        obj.setCatTicketTvmsimWechat(accTvmsimWechat);
                        obj.setCatTicketTvmsimXbank(accTvmsimXbank);
                        listDb.add(obj);

                    }
                    Map<String, Object> columnMap = new HashMap<>();
                    columnMap.put("CREATOR_ID", listDb.get(0).getCreatorId());
                    this.baseMapper.deleteByMap(columnMap);



                    //用多元化支付平台-微信支付宝银联（消费过闸）更新电子二维码车票数据
                    if(this.saveBatch(listDb)){
                        this.baseMapper.updateQr(listDb.get(0).getCreatorId());
                        this.baseMapper.updateTmpAndTotal(listDb.get(0).getCreatorId());
                        flag = true;
                    }
                }
                break;
            }
        }
        return flag;

    }

    private String getLineCode (List<Map<String,String>> lineMap,String lineName) {
        for (Map<String,String> map : lineMap){
            if(lineName.equals(map.get("NAME"))){
                return map.get("CODE");
            }
        }
        return "";
    }


    private void checkErr(SwCkOthenTicketRevenueManagementTmp obj, List<Map<String,String>> lineMap) {
        //更新字段
        try {
            String statisticsDateTmp = obj.getStatisticsDateTmp();  
			if(statisticsDateTmp!=null && statisticsDateTmp.length()==10){
				 Date statisticsDate = DateUtils.parseDate(statisticsDateTmp, parsePatternYMD);
                 obj.setStatisticsDate(statisticsDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
			}else{
				Date statisticsDate = DateUtils.parseDate(statisticsDateTmp, parsePattern);
                obj.setStatisticsDate(statisticsDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
			}
           
        } catch (Exception e) {
            obj.setErrMsg("交易日期 为空或格式错误");
        }

        if(StringUtils.isNotEmpty(obj.getLineCodeTmp())){
            String lineCode = getLineCode(lineMap,obj.getLineCodeTmp());
            if(StringUtils.isNotEmpty(lineCode)){
                obj.setLineCode(lineCode);
            } else {
                obj.setErrMsg("线路名称 为空或不规范");
            }

        } else {
            obj.setErrMsg("线路名称 为空");
        }

        try {
            BigDecimal num = new BigDecimal("100");

            String tvmOneTicketCashTmp = obj.getTvmOneTicketCashTmp();
            BigDecimal tvmOneTicketCash = new BigDecimal(tvmOneTicketCashTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            tvmOneTicketCash = tvmOneTicketCash.multiply(num);
            obj.setTvmOneTicketCash(tvmOneTicketCash.longValue());

            String tvmOneTicketAlipayTmp = obj.getTvmOneTicketAlipayTmp();
            BigDecimal tvmOneTicketAlipay = new BigDecimal(tvmOneTicketAlipayTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            tvmOneTicketAlipay = tvmOneTicketAlipay.multiply(num);
            obj.setTvmOneTicketAlipay(tvmOneTicketAlipay.longValue());

            String tvmOneTicketWechatTmp = obj.getTvmOneTicketWechatTmp();
            BigDecimal tvmOneTicketWechat = new BigDecimal(tvmOneTicketWechatTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            tvmOneTicketWechat = tvmOneTicketWechat.multiply(num);
            obj.setTvmOneTicketWechat(tvmOneTicketWechat.longValue());

            String tvmOneTicketXbankTmp = obj.getTvmOneTicketXbankTmp();
            BigDecimal tvmOneTicketXbank = new BigDecimal(tvmOneTicketXbankTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            tvmOneTicketXbank = tvmOneTicketXbank.multiply(num);
            obj.setTvmOneTicketXbank(tvmOneTicketXbank.longValue());

            String tvmSubTotalTmp = obj.getTvmSubTotalTmp();
            BigDecimal tvmSubTotal = new BigDecimal(tvmSubTotalTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            tvmSubTotal = tvmSubTotal.multiply(num);
            obj.setTvmSubTotal(tvmSubTotal.longValue());

            String bomOneTicketCashTmp = obj.getBomOneTicketCashTmp();
            BigDecimal bomOneTicketCash = new BigDecimal(bomOneTicketCashTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            bomOneTicketCash = bomOneTicketCash.multiply(num);
            obj.setBomOneTicketCash(bomOneTicketCash.longValue());

			String bomOnePosAmountTmp=obj.getBomOnePosAmountTmp();
            BigDecimal bomOnePosAmount = new BigDecimal(bomOnePosAmountTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            bomOnePosAmount = bomOnePosAmount.multiply(num);
            obj.setBomOnePosAmount(bomOnePosAmount.longValue());

            String bomOneTicketAlipayTmp = obj.getBomOneTicketPosTmp();
            BigDecimal bomOneTicketAlipay = new BigDecimal(bomOneTicketAlipayTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            bomOneTicketAlipay = bomOneTicketAlipay.multiply(num);
            obj.setBomOneTicketPos(bomOneTicketAlipay.longValue());

            String bomOneTicketWechatTmp = obj.getBomOneTicketWechatTmp();
            BigDecimal bomOneTicketWechat = new BigDecimal(bomOneTicketWechatTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            bomOneTicketWechat = bomOneTicketWechat.multiply(num);
            obj.setBomOneTicketWechat(bomOneTicketWechat.longValue());

            String bomSubTotalTmp = obj.getBomSubTotalTmp();
            BigDecimal bomSubTotal = new BigDecimal(bomSubTotalTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            bomSubTotal = bomSubTotal.multiply(num);
            obj.setBomSubTotal(bomSubTotal.longValue());

            String prefabricatedTicketAmountTmp = obj.getPrefabricatedTicketAmountTmp();
            BigDecimal prefabricatedTicketAmount = new BigDecimal(prefabricatedTicketAmountTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            prefabricatedTicketAmount = prefabricatedTicketAmount.multiply(num);
            obj.setPrefabricatedTicketAmount(prefabricatedTicketAmount.longValue());

			String prefabricatedTicketPosAmountTmp = obj.getPrefabricatedTicketPosAmountTmp();
            BigDecimal prefabricatedTicketPosAmount = new BigDecimal(prefabricatedTicketPosAmountTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            prefabricatedTicketPosAmount = prefabricatedTicketPosAmount.multiply(num);
            obj.setPrefabricatedTicketPosAmount(prefabricatedTicketPosAmount.longValue());

            String emergencyTicketAmountTmp = obj.getEmergencyTicketAmountTmp();
            BigDecimal emergencyTicketAmount = new BigDecimal(emergencyTicketAmountTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            emergencyTicketAmount = emergencyTicketAmount.multiply(num);
            obj.setEmergencyTicketAmount(emergencyTicketAmount.longValue());

            String ticketCountingTmp = obj.getTicketCountingTmp();
            BigDecimal ticketCounting = new BigDecimal(ticketCountingTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            ticketCounting = ticketCounting.multiply(num);
            obj.setTicketCounting(ticketCounting.longValue());

            String dailyTicketCashTmp = obj.getDailyTicketCashTmp();
            BigDecimal dailyTicketCash = new BigDecimal(dailyTicketCashTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            dailyTicketCash = dailyTicketCash.multiply(num);
            obj.setDailyTicketCash(dailyTicketCash.longValue());

			String dailyTicketPosAmountTmp = obj.getDailyTicketPosAmountTmp();
            BigDecimal dailyTicketPosAmount = new BigDecimal(dailyTicketPosAmountTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            dailyTicketPosAmount = dailyTicketPosAmount.multiply(num);
            obj.setDailyTicketPosAmount(dailyTicketPosAmount.longValue());

            String dailyTicketAlipayTmp = obj.getDailyTicketPosTmp();
            BigDecimal dailyTicketAlipay = new BigDecimal(dailyTicketAlipayTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            dailyTicketAlipay = dailyTicketAlipay.multiply(num);
            obj.setDailyTicketPos(dailyTicketAlipay.longValue());

            String dailyTicketWechatTmp = obj.getDailyTicketWechatTmp();
            BigDecimal dailyTicketWechat = new BigDecimal(dailyTicketWechatTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            dailyTicketWechat = dailyTicketWechat.multiply(num);
            obj.setDailyTicketWechat(dailyTicketWechat.longValue());

            String dailySubTotalTmp = obj.getDailySubTotalTmp();
            BigDecimal dailySubTotal = new BigDecimal(dailySubTotalTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            dailySubTotal = dailySubTotal.multiply(num);
            obj.setDailySubTotal(dailySubTotal.longValue());

            String commemorativeTicketCashTmp = obj.getCommemorativeTicketCashTmp();
            BigDecimal commemorativeTicketCash = new BigDecimal(commemorativeTicketCashTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            commemorativeTicketCash = commemorativeTicketCash.multiply(num);
            obj.setCommemorativeTicketCash(commemorativeTicketCash.longValue());

			String commemorativeTicketPosAmountTmp = obj.getCommemorativeTicketPosAmountTmp();
            BigDecimal commemorativeTicketPosAmount = new BigDecimal(commemorativeTicketPosAmountTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            commemorativeTicketPosAmount = commemorativeTicketPosAmount.multiply(num);
            obj.setCommemorativeTicketPosAmount(commemorativeTicketPosAmount.longValue());

            String commemorativeTicketAlipayTmp = obj.getCommemorativeTicketPosTmp();
            BigDecimal commemorativeTicketAlipay = new BigDecimal(commemorativeTicketAlipayTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            commemorativeTicketAlipay = commemorativeTicketAlipay.multiply(num);
            obj.setCommemorativeTicketPos(commemorativeTicketAlipay.longValue());

            String commemorativeTicketWechatTmp = obj.getCommemorativeTicketWechatTmp();
            BigDecimal commemorativeTicketWechat = new BigDecimal(commemorativeTicketWechatTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            commemorativeTicketWechat = commemorativeTicketWechat.multiply(num);
            obj.setCommemorativeTicketWechat(commemorativeTicketWechat.longValue());

            String commemorativeTicketSubTotalTmp = obj.getCommemorativeTicketSubTotalTmp();
            BigDecimal commemorativeTicketSubTotal = new BigDecimal(commemorativeTicketSubTotalTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            commemorativeTicketSubTotal = commemorativeTicketSubTotal.multiply(num);
            obj.setCommemorativeTicketSubTotal(commemorativeTicketSubTotal.longValue());

            String appCommemorativeTicketWechatTmp = obj.getAppCommemorativeTicketWechatTmp();
            BigDecimal appCommemorativeTicketWechat = new BigDecimal(appCommemorativeTicketWechatTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            appCommemorativeTicketWechat = appCommemorativeTicketWechat.multiply(num);
            obj.setAppCommemorativeTicketWechat(appCommemorativeTicketWechat.longValue());

            String catTicketCashTmp = obj.getCatTicketCashTmp();
            BigDecimal catTicketCash = new BigDecimal(catTicketCashTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            catTicketCash = catTicketCash.multiply(num);
            obj.setCatTicketCash(catTicketCash.longValue());

			String catTicketPosAmountTmp = obj.getCatTicketPosAmountTmp();
            BigDecimal catTicketPosAmount = new BigDecimal(catTicketPosAmountTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            catTicketPosAmount = catTicketPosAmount.multiply(num);
            obj.setCatTicketPosAmount(catTicketPosAmount.longValue());

            String catTicketAlipayTmp = obj.getCatTicketPosTmp();
            BigDecimal catTicketAlipay = new BigDecimal(catTicketAlipayTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            catTicketAlipay = catTicketAlipay.multiply(num);
            obj.setCatTicketPos(catTicketAlipay.longValue());

            String catTicketWechatTmp = obj.getCatTicketWechatTmp();
            BigDecimal catTicketWechat = new BigDecimal(catTicketWechatTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            catTicketWechat = catTicketWechat.multiply(num);
            obj.setCatTicketWechat(catTicketWechat.longValue());



            String catTicketTvmsimAlipayTmp = obj.getCatTicketTvmsimAlipayTmp();
            BigDecimal catTicketTvmsimAlipay = new BigDecimal(catTicketTvmsimAlipayTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            catTicketTvmsimAlipay = catTicketTvmsimAlipay.multiply(num);
            obj.setCatTicketTvmsimAlipay(catTicketTvmsimAlipay.longValue());

            String catTicketTvmsimWechatTmp = obj.getCatTicketTvmsimWechatTmp();
            BigDecimal catTicketTvmsimWechat = new BigDecimal(catTicketTvmsimWechatTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            catTicketTvmsimWechat = catTicketTvmsimWechat.multiply(num);
            obj.setCatTicketTvmsimWechat(catTicketTvmsimWechat.longValue());

            String catTicketTvmsimXbankTmp = obj.getCatTicketTvmsimXbankTmp();
            BigDecimal catTicketTvmsimXbank = new BigDecimal(catTicketTvmsimXbankTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            catTicketTvmsimXbank = catTicketTvmsimXbank.multiply(num);
            obj.setCatTicketTvmsimXbank(catTicketTvmsimXbank.longValue());


            String catSubTotalTmp = obj.getCatSubTotalTmp();
            BigDecimal catSubTotal = new BigDecimal(catSubTotalTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            catSubTotal = catSubTotal.multiply(num);
            obj.setCatSubTotal(catSubTotal.longValue());

            String catTicketPayCardTmp = obj.getCatTicketPayCardTmp();
            BigDecimal catTicketPayCard = new BigDecimal(catTicketPayCardTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            catTicketPayCard = catTicketPayCard.multiply(num);
            obj.setCatTicketPayCard(catTicketPayCard.longValue());

            String onlineTicketSalesTmp = obj.getOnlineTicketSalesTmp();
            BigDecimal onlineTicketSales = new BigDecimal(onlineTicketSalesTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            onlineTicketSales = onlineTicketSales.multiply(num);
            obj.setOnlineTicketSales(onlineTicketSales.longValue());

            String qrCodeTicketAlipayTmp = obj.getQrCodeTicketAlipayTmp();
            BigDecimal qrCodeTicketAlipay = new BigDecimal(qrCodeTicketAlipayTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            qrCodeTicketAlipay = qrCodeTicketAlipay.multiply(num);
            obj.setQrCodeTicketAlipay(qrCodeTicketAlipay.longValue());

            String qrCodeTicketWechatTmp = obj.getQrCodeTicketWechatTmp();
            BigDecimal qrCodeTicketWechat = new BigDecimal(qrCodeTicketWechatTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            qrCodeTicketWechat = qrCodeTicketWechat.multiply(num);
            obj.setQrCodeTicketWechat(qrCodeTicketWechat.longValue());

            String qrCodeTicketUnionpayTmp = obj.getQrCodeTicketUnionpayTmp();
            BigDecimal qrCodeTicketUnionpay = new BigDecimal(qrCodeTicketUnionpayTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            qrCodeTicketUnionpay = qrCodeTicketUnionpay.multiply(num);
            obj.setQrCodeTicketUnionpay(qrCodeTicketUnionpay.longValue());

            String qrCodeSubTotalTmp = obj.getQrCodeSubTotalTmp();
            BigDecimal qrCodeSubTotal = new BigDecimal(qrCodeSubTotalTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            qrCodeSubTotal = qrCodeSubTotal.multiply(num);
            obj.setQrCodeSubTotal(qrCodeSubTotal.longValue());

            String faceEleBankTmp = obj.getFaceEleBankTmp();
            BigDecimal faceEleBank = new BigDecimal(faceEleBankTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            faceEleBank = faceEleBank.multiply(num);
            obj.setFaceEleBank(faceEleBank.longValue());

            String qrConnectivityAlipayTmp = obj.getQrConnectivityAlipayTmp();
            BigDecimal qrConnectivityAlipay = new BigDecimal(qrConnectivityAlipayTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            qrConnectivityAlipay = qrConnectivityAlipay.multiply(num);
            obj.setQrConnectivityAlipay(qrConnectivityAlipay.longValue());

            String qrConnectivityWechatTmp = obj.getQrConnectivityWechatTmp();
            BigDecimal qrConnectivityWechat = new BigDecimal(qrConnectivityWechatTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            qrConnectivityWechat = qrConnectivityWechat.multiply(num);
            obj.setQrConnectivityWechat(qrConnectivityWechat.longValue());

            String qrConnectivitySubTotalTmp = obj.getQrConnectivitySubTotalTmp();
            BigDecimal qrConnectivitySubTotal = new BigDecimal(qrConnectivitySubTotalTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            qrConnectivitySubTotal = qrConnectivitySubTotal.multiply(num);
            obj.setQrConnectivitySubTotal(qrConnectivitySubTotal.longValue());

            String allOneTrafficCardTmp = obj.getAllOneTrafficCardTmp();
            BigDecimal allOneTrafficCard = new BigDecimal(allOneTrafficCardTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            allOneTrafficCard = allOneTrafficCard.multiply(num);
            obj.setAllOneTrafficCard(allOneTrafficCard.longValue());

            String stationTicketLongCashTmp = obj.getStationTicketLongCashTmp();
            BigDecimal stationTicketLongCash = new BigDecimal(stationTicketLongCashTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            stationTicketLongCash = stationTicketLongCash.multiply(num);
            obj.setStationTicketLongCash(stationTicketLongCash.longValue());

            String stationTicketShortCashTmp = obj.getStationTicketShortCashTmp();
            BigDecimal stationTicketShortCash = new BigDecimal(stationTicketShortCashTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            stationTicketShortCash = stationTicketShortCash.multiply(num);
            obj.setStationTicketShortCash(stationTicketShortCash.longValue());

            String stationCompensationCashTmp = obj.getStationCompensationCashTmp();
            BigDecimal stationCompensationCash = new BigDecimal(stationCompensationCashTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            stationCompensationCash = stationCompensationCash.multiply(num);
            obj.setStationCompensationCash(stationCompensationCash.longValue());

            String releasePettyCashTmp = obj.getReleasePettyCashTmp();
            BigDecimal releasePettyCash = new BigDecimal(releasePettyCashTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            releasePettyCash = releasePettyCash.multiply(num);
            obj.setReleasePettyCash(releasePettyCash.longValue());

			String subTotalPosTmp = obj.getSubTotalPosTmp();
            BigDecimal subTotalPos = new BigDecimal(subTotalPosTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            subTotalPos = subTotalPos.multiply(num);
            obj.setSubTotalPos(subTotalPos.longValue());

            String settlementAmountTmp = obj.getSettlementAmountTmp();
            BigDecimal settlementAmount = new BigDecimal(settlementAmountTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            settlementAmount = settlementAmount.multiply(num);
            obj.setSettlementAmount(settlementAmount.longValue());

            String subTotalTmp = obj.getSubTotalTmp();
            BigDecimal subTotal = new BigDecimal(subTotalTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            subTotal = subTotal.multiply(num);
            obj.setSubTotal(subTotal.longValue());


        } catch (Exception e) {
            obj.setErrMsg("金额 为空或格式错误");
        }


        //判断数据是否存在
        SwCkOthenTicketRevenueManagement swCkOthenTicketRevenueManagement = new SwCkOthenTicketRevenueManagement();
        //BeanUtils.copyProperties(obj, swCkOthenTicketRevenueManagement, "deptId","creatorId","creatorName","createTime");
        swCkOthenTicketRevenueManagement.setStatisticsDate(obj.getStatisticsDate());
        swCkOthenTicketRevenueManagement.setLineCode(obj.getLineCode());
        List<SwCkOthenTicketRevenueManagement> list = swCkOthenTicketRevenueManagementService.list(Wrappers.query(swCkOthenTicketRevenueManagement));
        if (list != null && !list.isEmpty()) {
            obj.setErrMsg("数据已存在");
        }
    }



    private boolean parseDate(String string) {
        boolean convertSuccess = true;
        if (string == null) {
            return false;
        }

        try {
			if(string!=null && string.length()==10){
				 DateUtils.parseDate(string, parsePatternYMD);				
			}else{
				DateUtils.parseDate(string, parsePattern);
			}
           
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    @Override
    public void saveBatchWithExcelOk(UserDto user) {
        Map<String,String> map = this.baseMapper.selectMaxMinStatisticsDate(user.getId());
        if(map == null) {
            return;
        }
        String minStatisticsDate = map.get("minStatisticsDate");
        String maxStatisticsDate = map.get("maxStatisticsDate");

        SwCkOthenTicketRevenueManagementLog swCkOthenTicketRevenueManagementLog = new SwCkOthenTicketRevenueManagementLog();
        swCkOthenTicketRevenueManagementLog.setServiceId(getServiceIdByLineCode("54"));
        swCkOthenTicketRevenueManagementLog.setBeginTime(LocalDate.parse(minStatisticsDate, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        swCkOthenTicketRevenueManagementLog.setEndTime(LocalDate.parse(maxStatisticsDate, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        swCkOthenTicketRevenueManagementLog.setSubmitPersonId(user.getId());
        swCkOthenTicketRevenueManagementLog.setSubmitPersonCode(user.getUserCode());
        swCkOthenTicketRevenueManagementLog.setSubmitPersonName(user.getNikeName());
        swCkOthenTicketRevenueManagementLog.setCreatorId(user.getId());
        swCkOthenTicketRevenueManagementLog.setDeptId(user.getDeptId());
        swCkOthenTicketRevenueManagementLog.setCreatorName(user.getNikeName());
        String uuid = UUID.randomUUID().toString().toLowerCase();
        swCkOthenTicketRevenueManagementLog.setUuid(uuid);

        swCkOthenTicketRevenueManagementLogService.save(swCkOthenTicketRevenueManagementLog);
        //导入先删除
        this.baseMapper.deleteBatchWithExcelOk(user.getId());
        this.baseMapper.saveBatchWithExcelOk(user.getId(),uuid);
        saveBatchWithExcelCancel(user.getId());
    }

    @Override
    public void saveBatchWithExcelCancel(Integer creatorId) {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("CREATOR_ID", creatorId);
        this.baseMapper.deleteByMap(columnMap);
    }

    private String getServiceIdByLineCode(String lineCode){
        String sql = "select serviceid from sw_param_line where code='"+lineCode+"'";
        try{
            List<Map<String,Object>> list = JdbcUtil.exeSqlQuery(sql);
            if(list!=null && list.size()>0){
                Map<String,Object> map = list.get(0);
                return String.valueOf(map.get("serviceid"));
            }
        }catch(Exception e){
            return null;
        }

        return null;
    }
}
